<?php


namespace app\common\model;


use app\common\model\system\Config as ConfigModel;
use app\common\model\system\Log;
use app\dock\model\User;
use mb\helper\storage\TentCos;
use think\facade\Cache;
use mb\helper\Core;
use think\facade\Lang;

class Base
{
    public static function handlePassword($password, $salt)
    {
        $password = md5($password);
        return crypt($password, $salt);
    }

    public static function addMemberLog($userId, $action, $targetId = 0, $params = [])
    {
        $params['ip'] = self::getClientIp();
        $newRow = [
            'type' => 'member',
            'userId' => $userId,
            'targetId' => $targetId,
            'action' => $action,
            'memo' => serialize($params),
        ];
        Log::add($newRow);
    }

    /*
     *
     */
    public static function addMoneyLog($row,$connect = null)
    {
        $newRow = [
            'memberId' => $row['memberId'],
            'targetId' => $row['targetId'],
            'afterMoney' => $row['memberMoney'],
            'money' => $row['money'],
            'type' => $row['type'],
            'ghost' => $row['memberGhost'],
            'msg' => empty($row['msg'])? '' : $row['msg'],
            'existence' => !isset($row['existence'])? '1' : $row['existence'],
            'adminId' => empty($row['adminId'])? '' : $row['adminId'],

        ];
        $result = \app\common\model\member\money\Log::add($newRow,$connect);
        return $result;
    }

    public static function getOrderNo()
    {
        return (date("YmdHms") . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9));
    }

    /**
     * @return TentCos
     */
    public static function createTentCos()
    {
 $params = [
            'secretId' => 'AKIDjPtBG8mANQGukPp0wc4QZS8ZmlV1KQIv',
            'secretKey' => 'ykHj055rTvA9vRoysHTRFpPuFNIyRlvw',
            'region' => 'ap-hongkong',
            'appId' => '1311711508',
            'bucket' => 'xingshi'
        ];
        return new TentCos($params);
    }


    public static function getTg()
    {
        return [
            // 'api' => '16634789601748',
            // 'key' => '3dcb2d273bf60ffb02d61ab2036a7b73',
            // 'type' => 'ag',
            // 'url' => 'http://tggood.com/122'
        ];
    }

    public static function getClientIp($type = 0, $adv = false)
    {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL) return $ip[$type];
        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos) unset($arr[$pos]);
                $ip = trim($arr[0]);
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['HTTP_XIP'])) {
            $ip = $_SERVER['HTTP_XIP'];
        }
// IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

    /**
     * 获取系统配置
     * @param $field
     */
    public static function getSysConfig($field)
    {
        $result = ConfigModel::search([]);
        $data = [];
        foreach ($result as $v) {
            $data[$v['field']] = $v['value'];
        }
        if (empty($data[$field])) {
            return false;
        }
        return $data[$field];
    }

    public static function toCn($array)
    {
        $returnSet = empty($array)?[]:$array;
        $lang = Lang::getLangSet();
        if ($lang != 'cn') {
            $file = include base_path().'dock/lang/'.$lang.'.php';
            $newLang = [];
            foreach ($file as $k=>$f) {
                $newLang[$f] = $k;
            }
            foreach ($returnSet as &$value){
                $value = empty($newLang[$value])?$value:$newLang[$value];
            }
        }
        return $returnSet;
    }

  public static function getNgApi()
    {
        $result = [
            'sn' => 'tc5',
            'secretKey' => 'V3f41d10e4F1839P9Mx6K9s36Li8048U',
            'random' => Core::random(16),
        ];
        $result['sign'] = md5($result['random'].$result['sn'].$result['secretKey']);
        return $result;
    }
}